package com.xjtu.platform.user.admin.visit_arrange.impl;

import com.xjtu.platform.common.Utils;
import com.xjtu.platform.common.model.User;
import com.xjtu.platform.user.admin.visit_arrange.AdminDto;
import com.xjtu.platform.user.admin.visit_arrange.AdminService;
import com.xjtu.platform.user.admin.visit_arrange.dao.AdminDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @创建人 MBY381
 * @创建时间 2022/7/2 09:06
 * @描述 暂无
 */


@Service
@Transactional
public class AdminServiceImpl implements AdminService {


    @Resource
    AdminDao adminDao;
    @Override
    public Map<String, Object> getInfo(AdminDto adminDto) {

        return Utils.getPage(adminDto,()->
            adminDao.getPage()
        );
    }

    @Override
    public List<User> getFreeConsultant() {
        return adminDao.getFreeConsultant();
    }

    @Override
    public void updateConsultant(User visitor, String student) throws Exception {
        adminDao.updateArrange(visitor.getUser_id(),student);
        String studentEmail =adminDao.findEmailByID(student);
        String visitorEmail = adminDao.findEmailByID(visitor.getUser_id());
        new Thread(()->{
            try {
                if(visitorEmail==null||studentEmail==null){
                    throw new RuntimeException("有邮箱为空,无法发送");
                }
                Utils.sendMail(visitorEmail,"您有个学生需要去出访哦！\n学生:"+student+"联系方式:"+studentEmail);
                Utils.sendMail(studentEmail,student+"同学您好！已为您安排出访！\n老师:"+visitor.getUser_id()+"联系方式:"+visitor.getEmail()+"请您与老师联系协商出访时间~");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    @Override
    public void informStudentOutOfVisitor(String user_id) {
        try {

            String email=adminDao.findEmailByID(user_id);

            new Thread(()->{
                try {
                    Utils.sendMail(email,"同学您好，由于目前咨询资源的紧凑，现在没有咨询师可供您预约。\n我们将尽快安排更多资源，为您安排预约，请耐心等待！\n");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
